package cn.edu.sasu.controller;

import cn.edu.sasu.common.base.LayuiRespResult;
import cn.edu.sasu.common.base.RespResult;

import cn.edu.sasu.domain.entity.CaseManager;
import cn.edu.sasu.domain.vo.CaseManagervo;
import cn.edu.sasu.service.CaseManagerService;

import net.bytebuddy.agent.builder.AgentBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * (CaseManager)表控制层
 *
 * @author makejava
 * @since 2024-04-28 09:42:49
 */
@RestController
@RequestMapping("/caseManager")
public class CaseManagerController {
    /**
     * 服务对象
     */
    @Resource
    private CaseManagerService caseManagerService;

    @PostMapping("/batchdel")
    public RespResult batchdel(Integer[] ids){
        return caseManagerService.batchdel(ids);
    }


    /**
     * 分页查询
     *
     * @param limit 筛选条件
     * @param page      分页对象
     * @return 查询结果
     */
    @GetMapping("/findAll")
    public LayuiRespResult findHospitalAll(Integer page, Integer limit){
        return caseManagerService.caselist(page,limit);
    }
//    @GetMapping("/findAllCase")
//    public LayuiRespResult findAllCase(Integer page, Integer limit,Integer doctorId){
//        return caseManagerService.findAllCase(page,limit,doctorId);
//    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<CaseManager> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.caseManagerService.queryById(id));
    }
    @GetMapping("/findAllCase")
    public LayuiRespResult findAllCase(Integer page, Integer limit,Integer doctorId){
        return caseManagerService.findAllCase(page,limit,doctorId);
    }
    /**
     * 新增数据
     *
     * @param caseManager 实体
     * @return 新增结果
     */
    @PostMapping
    @RequestMapping("/add")
    public ResponseEntity<CaseManager> add(@RequestBody CaseManager caseManager) {
        System.out.println(caseManager.getCasename());
        caseManager.setStatus("0");
        caseManager.setCancelstatus("1");
        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 使用 format 方法将 LocalDateTime 转换为字符串
        String formattedDateTime = now.format(formatter);

        // 将字符串解析回 LocalDateTime
        LocalDateTime parsedDateTime = LocalDateTime.parse(formattedDateTime, formatter);

        // 将 LocalDateTime 转换为 Instant（需要指定时区，因为 LocalDateTime 不包含时区信息）
        ZoneId zoneId = ZoneId.systemDefault(); // 使用系统默认时区
        java.time.Instant instant = parsedDateTime.atZone(zoneId).toInstant();

        // 从 Instant 创建 Date 对象
        Date date = Date.from(instant);
        caseManager.setBuildtime(date);
        return ResponseEntity.ok(this.caseManagerService.insert(caseManager));
    }

    /**
     * 编辑数据
     *
     * @param caseManager 实体
     * @return 编辑结果
     */
    @PostMapping
    @RequestMapping("/edit")
    public ResponseEntity<CaseManager> edit(@RequestBody CaseManager caseManager) {

        return ResponseEntity.ok(this.caseManagerService.update(caseManager));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @RequestMapping("/delete/{id}")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.caseManagerService.deleteById(id));
    }

    @GetMapping("/search")
    public RespResult search(CaseManagervo caseManager){
        System.out.println(caseManager);
//        return RespResult.success();
        return RespResult.success(this.caseManagerService.search(caseManager));
    }
}

